"""
Case Type   : 兼容M*数据库sec_to_time函数
Case Name   : sec_to_time函数参数有效取值测试
Create At   : 2022.11.25
Owner       : z@wan005
Description :
    1、参数值返回值覆盖time类型的左右边界
    2、参数为0值
    3、参数值含微秒
    4、清理环境
Expect      :
    1、成功
    2、成功
    3、超过6位，四舍五入进位
    4、成功
History     :modified by zhanghuan96，2024.06.28，优化断言
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest01(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_sec_to_time_0001"

    def test_compatibility(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.log.info('----前置条件：创建兼容b库---')
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res)

        text = '--step1:参数值返回值覆盖time类型的左右边界;expect:成功--'
        self.log.info(text)
        sql_cmd = ("select sec_to_time(3020399);"
                   "select sec_to_time(-3020399);")
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 8, '行数不到9行')
        self.assertEqual('838:59:59', sql_res.splitlines()[2].strip(),
                         '执行失败' + text)
        self.assertEqual('-838:59:59', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = '--step2:参数为0值;expect:成功--'
        self.log.info(text)
        sql_cmd = "select sec_to_time(0);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertEqual('00:00:00', sql_res.splitlines()[2].strip(),
                         '执行失败' + text)

        text = '--step3:参数值含微秒;expect:超过6位，四舍五入进位--'
        self.log.info(text)
        sql_cmd = "select sec_to_time(3020398.9);" \
                  "select sec_to_time(3020398.99);" \
                  "select sec_to_time(3020398.999);" \
                  "select sec_to_time(3020398.9999);" \
                  "select sec_to_time(3020398.99999);" \
                  "select sec_to_time(3020398.999999);" \
                  "select sec_to_time(3020398.9999999);" \
                  "select sec_to_time(2378.2222225);" \
                  "select sec_to_time(60.4444445);" \
                  "select sec_to_time(1000000.4999994999);" \
                  "select sec_to_time(1000000.499999499);" \
                  "select sec_to_time(1000000.499);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 59, '行数不到59行')
        self.assertEqual('838:59:58.9', sql_res.splitlines()[2].strip(),
                         '执行失败' + text)
        self.assertEqual('838:59:58.99', sql_res.splitlines()[7].strip(),
                         '执行失败' + text)
        self.assertEqual('838:59:58.999', sql_res.splitlines()[12].strip(),
                         '执行失败' + text)
        self.assertEqual('838:59:58.9999', sql_res.splitlines()[17].strip(),
                         '执行失败' + text)
        self.assertEqual('838:59:58.99999', sql_res.splitlines()[22].strip(),
                         '执行失败' + text)
        self.assertEqual('838:59:58.999999', sql_res.splitlines()[27].strip(),
                         '执行失败' + text)
        self.assertEqual('838:59:59', sql_res.splitlines()[32].strip(),
                         '执行失败' + text)
        self.assertEqual('00:39:38.222223', sql_res.splitlines()[37].strip(),
                         '执行失败' + text)
        self.assertEqual('00:01:00.444445', sql_res.splitlines()[42].strip(),
                         '执行失败' + text)
        self.assertEqual('277:46:40.499999', sql_res.splitlines()[47].strip(),
                         '执行失败' + text)
        self.assertEqual('277:46:40.499999', sql_res.splitlines()[-7].strip(),
                         '执行失败' + text)
        self.assertEqual('277:46:40.499', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

    def tearDown(self):
        text = '----step7:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database {self.db_name};"
        self.log.info(sql_cmd)
        drop_res1 = self.sh_primary.execut_db_sql(sql_cmd,
                                                  dbname='postgres')
        self.log.info(drop_res1)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res1,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
